﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using IP.Core.IPData;
using IP.Core.IPUserService;
using WebDS.CDBNames;
using WebDS;
using IP.Core.IPCommon;
using WebUS;

public partial class ChucNang_F614_CapNhatGiangVienVaoSuKien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            decimal v_dc_id_nhom = CIPConvert.ToDecimal(Session["user_quyen"]);
            if (!PhanQuyenUtils.is_having_quyen_page("F614_CapNhatGiangVienVaoSuKien.aspx", v_dc_id_nhom))
                Response.Redirect("/TRMProject/Default.aspx");
            load_data_2_cbo_loai_su_kien();
            load_2_cbo_su_kien();
            load_2_cbo_vai_tro_giang_vien();
            load_2_cbo_don_vi_q_ly();
            load_cbo_ds_gv_chua_add_vao_su_kien(m_txt_ho_ten_giang_vien.Text, CIPConvert.ToDecimal(m_cbo_don_vi_quan_ly.SelectedValue));
            load_cbo_ds_gv_da_add_vao_su_kien();
        }    
    }

    #region Members
    US_DM_SU_KIEN m_us_dm_su_kien = new US_DM_SU_KIEN();
    DS_DM_SU_KIEN m_ds_dm_su_kien = new DS_DM_SU_KIEN();
    #endregion

    #region Public Interface

    #endregion

    #region Private Methods
    /// <summary>
    /// Load danh sách các giảng viên chưa đc add vào sự kiện
    /// </summary>
    private void load_cbo_ds_gv_chua_add_vao_su_kien(string ip_str_ho_ten_gv_search
                                                    , decimal ip_dc_id_dv_quan_ly_search)
    {
        US_V_DM_GIANG_VIEN v_us_giang_vien = new US_V_DM_GIANG_VIEN();
        DS_V_DM_GIANG_VIEN v_ds_giang_vien = new DS_V_DM_GIANG_VIEN();
        v_us_giang_vien.load_ds_giang_vien_chua_add_vao_su_kien(v_ds_giang_vien
                                                                , CIPConvert.ToDecimal(m_cbo_su_kien.SelectedValue)
                                                                , ip_str_ho_ten_gv_search
                                                                , ip_dc_id_dv_quan_ly_search);
        m_lst_gv_chua_add.Items.Clear();
        for (int v_i = 0; v_i < v_ds_giang_vien.V_DM_GIANG_VIEN.Rows.Count; v_i++)
        {
            if(!is_item_giang_vien_added(CIPConvert.ToDecimal(v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.ID])))
                m_lst_gv_chua_add.Items.Add(new ListItem(v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.HO_VA_TEN_DEM].ToString().TrimEnd() + " " + v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.TEN_GIANG_VIEN].ToString() + " - Mã GV: " + v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.MA_GIANG_VIEN].ToString()
                                            , v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.ID].ToString()));
        }
    }
    private bool is_item_giang_vien_added(decimal ip_dc_id_giang_vien)
    {
        for (int v_i = 0; v_i < m_lst_gv_da_add.Items.Count; v_i++)
        {
            if (m_lst_gv_da_add.Items[v_i].Value == ip_dc_id_giang_vien.ToString()) return true;
        }
        return false;
    }
    private void load_cbo_ds_gv_da_add_vao_su_kien()
    {
        US_V_DM_GIANG_VIEN v_us_giang_vien = new US_V_DM_GIANG_VIEN();
        DS_V_DM_GIANG_VIEN v_ds_giang_vien = new DS_V_DM_GIANG_VIEN();
        //v_us_giang_vien.FillDataset(v_ds_giang_vien, " WHERE ID NOT IN (SELECT ID_GIANG_VIEN FROM GD_ASSIGN_SU_KIEN_GIANG_VIEN WHERE ID_SU_KIEN =" + CIPConvert.ToDecimal(m_cbo_su_kien.SelectedValue) + ")");
        v_us_giang_vien.load_ds_giang_vien_da_add_vao_su_kien(v_ds_giang_vien, CIPConvert.ToDecimal(m_cbo_su_kien.SelectedValue));
        m_lst_gv_da_add.Items.Clear();
        for (int v_i = 0; v_i < v_ds_giang_vien.V_DM_GIANG_VIEN.Rows.Count; v_i++)
        {
            m_lst_gv_da_add.Items.Add(new ListItem(v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.HO_VA_TEN_DEM].ToString().TrimEnd() + " " + v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.TEN_GIANG_VIEN].ToString() + " - Mã GV: " + v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.MA_GIANG_VIEN].ToString(), v_ds_giang_vien.V_DM_GIANG_VIEN.Rows[v_i][V_DM_GIANG_VIEN.ID].ToString()));
        }
    }
    private void load_data_2_cbo_loai_su_kien()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien, " WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.LOAI_SU_KIEN);

        m_cbo_loai_su_kien.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_loai_su_kien.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_loai_su_kien.DataSource = v_ds_tu_dien.CM_DM_TU_DIEN;
        m_cbo_loai_su_kien.DataBind();
    }
    private void load_2_cbo_vai_tro_giang_vien()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien, " WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.VAI_TRO_GV);

        m_cbo_vai_tro_gv.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_vai_tro_gv.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_vai_tro_gv.DataSource = v_ds_tu_dien.CM_DM_TU_DIEN;
        m_cbo_vai_tro_gv.DataBind();
    }
    private void load_2_cbo_su_kien()
    {
        US_DM_SU_KIEN v_us_su_kien = new US_DM_SU_KIEN();
        DS_DM_SU_KIEN v_ds_su_kien = new DS_DM_SU_KIEN();

        v_us_su_kien.FillDataset(v_ds_su_kien, " WHERE ID_LOAI_SU_KIEN=" + CIPConvert.ToDecimal(m_cbo_loai_su_kien.SelectedValue) + " ORDER BY ID DESC");

        m_cbo_su_kien.DataTextField = DM_SU_KIEN.TEN_SU_KIEN;
        m_cbo_su_kien.DataValueField = DM_SU_KIEN.ID;

        m_cbo_su_kien.DataSource = v_ds_su_kien.DM_SU_KIEN;
        m_cbo_su_kien.DataBind();
    }
    private void load_2_cbo_don_vi_q_ly()
    {
        try
        {
            DS_CM_DM_TU_DIEN v_ds_cm_tu_dien = new DS_CM_DM_TU_DIEN();
            US_CM_DM_TU_DIEN v_us_cm_tu_dien = new US_CM_DM_TU_DIEN();

            // Đổ dữ liệu vào DS 
            v_us_cm_tu_dien.Load_Dv_quan_ly(v_ds_cm_tu_dien, CIPConvert.ToStr(Session["UserName"]));

            m_cbo_don_vi_quan_ly.Items.Add(new ListItem("Tất cả", "0"));
            for (int i = 0; i < v_ds_cm_tu_dien.CM_DM_TU_DIEN.Rows.Count; i++)
            {
                m_cbo_don_vi_quan_ly.Items.Add(new ListItem(v_ds_cm_tu_dien.CM_DM_TU_DIEN[i][CM_DM_TU_DIEN.TEN].ToString(), v_ds_cm_tu_dien.CM_DM_TU_DIEN[i][CM_DM_TU_DIEN.ID].ToString()));
            }
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void update_giang_vien_vao_su_kien()
    {
        try
        {
            m_lbl_mess.Text = "";
            decimal v_dc_id_su_kien = CIPConvert.ToDecimal(m_cbo_su_kien.SelectedValue);
            decimal v_dc_id_vai_tro = CIPConvert.ToDecimal(m_cbo_vai_tro_gv.SelectedValue);
            decimal v_dc_so_tien_gv_huong = CIPConvert.ToDecimal(m_txt_so_tien_gv_huong.Text.Trim());
            string v_str_thanh_toan_ngay_yn = "";
            if (m_rdl_thanh_toan_ngay.Items[0].Selected) v_str_thanh_toan_ngay_yn = "N";
            else v_str_thanh_toan_ngay_yn = "Y";
            string v_str_id_giang_vien_s = "";
            foreach (ListItem ltTemp in this.m_lst_gv_da_add.Items)
            {

                v_str_id_giang_vien_s += ltTemp.Value + ",";
            }
            US_V_GD_ASSIGN_SU_KIEN_GIANG_VIEN v_us_assign_gv_su_kien = new US_V_GD_ASSIGN_SU_KIEN_GIANG_VIEN();
            v_us_assign_gv_su_kien.update_giang_vien_su_kien( v_dc_id_su_kien
                                                            , v_str_id_giang_vien_s
                                                            , v_dc_id_vai_tro
                                                            , v_dc_so_tien_gv_huong
                                                            , v_str_thanh_toan_ngay_yn
                                                            , CIPConvert.ToStr(Session["Username"]));
            m_lbl_mess.Text = "Assign giảng viên vào sự kiện thành công";
            reset_controls();
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình cập nhật bản ghi";
            throw v_e;
        }
    }

    private void reset_controls()
    {
        m_txt_so_tien_gv_huong.Text = "";
        m_txt_ho_ten_giang_vien.Text = "";
        m_cbo_don_vi_quan_ly.SelectedIndex = 0;
        load_cbo_ds_gv_chua_add_vao_su_kien(m_txt_ho_ten_giang_vien.Text, CIPConvert.ToDecimal(m_cbo_don_vi_quan_ly.SelectedValue));
        load_cbo_ds_gv_da_add_vao_su_kien();
        m_rdl_thanh_toan_ngay.Items[0].Selected = true;
    }
    #endregion

    #region Events
    protected void m_cmd_right_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_gv_chua_add.Items.Count > 0 && m_lst_gv_chua_add.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_gv_chua_add.SelectedItem;
                selectedItem.Selected = false;
                m_lst_gv_da_add.Items.Add(selectedItem);
                m_lst_gv_chua_add.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_right_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_gv_chua_add.Items)
            {
                this.m_lst_gv_da_add.Items.Add(ltTemp);
            }
            this.m_lst_gv_chua_add.Items.Clear();

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_gv_da_add.Items.Count > 0 && m_lst_gv_da_add.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_gv_da_add.SelectedItem;
                selectedItem.Selected = false;
                m_lst_gv_chua_add.Items.Add(selectedItem);
                m_lst_gv_da_add.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_gv_da_add.Items)
            {
                this.m_lst_gv_chua_add.Items.Add(ltTemp);
            }
            this.m_lst_gv_da_add.Items.Clear();


        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_su_kien_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_cbo_ds_gv_chua_add_vao_su_kien(m_txt_ho_ten_giang_vien.Text, CIPConvert.ToDecimal(m_cbo_don_vi_quan_ly.SelectedValue));
            load_cbo_ds_gv_da_add_vao_su_kien();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("/TRMProject/DanhMuc/F105_SuKien.aspx");
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_loai_su_kien_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_2_cbo_su_kien();
        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        try
        {
            update_giang_vien_vao_su_kien();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    #endregion
    protected void m_cmd_tim_kiem_Click(object sender, EventArgs e)
    {
        try
        {
            load_cbo_ds_gv_chua_add_vao_su_kien(m_txt_ho_ten_giang_vien.Text
                                                , CIPConvert.ToDecimal(m_cbo_don_vi_quan_ly.SelectedValue));
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_don_vi_quan_ly_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_cbo_ds_gv_chua_add_vao_su_kien(m_txt_ho_ten_giang_vien.Text, CIPConvert.ToDecimal(m_cbo_don_vi_quan_ly.SelectedValue));
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
}